import os
import sys

def bin_to_c_array(bin_file, output_file):
    with open(bin_file, 'rb') as f:
        binary_data = f.read()

    # 获取文件名，作为数组的变量名
    array_name = os.path.splitext(os.path.basename(bin_file))[0]
    
    # 开始生成 C 语言数组的内容
    c_array = f"unsigned char {array_name}[] = {{\n"
    for i, byte in enumerate(binary_data):
        if i % 12 == 0:  # 每行12个字节
            c_array += "\n   "
        c_array += f"0x{byte:02X}, "
    c_array = c_array.rstrip(', ') + "\n};\n"

    # 将数组写入到 output.h 文件中
    with open(output_file, 'w') as f:
        f.write(f"#ifndef {array_name.upper()}_H\n")
        f.write(f"#define {array_name.upper()}_H\n\n")
        f.write(c_array)
        f.write(f"\n#endif // {array_name.upper()}_H\n")

    print(f"Generated {output_file} with C array.")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} <binary file path>")
        sys.exit(1)
    
    bin_file = sys.argv[1]
    output_file = "output.h"
    
    bin_to_c_array(bin_file, output_file)
